package part1.task3;

public class Producer implements Runnable {
    private final Buffer<Object> buffer;
    private final String name;

    public Producer(String name, Buffer<Object> buffer) {
        this.buffer = buffer;
        this.name=name;
    }

    @Override
    public void run() {
        Object product=null;
        while (!Thread.currentThread().isInterrupted()) {
            if (product==null)
                product=new Object();
            synchronized (buffer) {
                if (buffer.add(product)){
                    product=null;
                    buffer.notifyAll();
                    System.out.println("Producer "+name+" add.");
                } else {
                    try {
                        System.out.println("Producer "+name+" wait.");
                        buffer.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
